package com.example.toutiao.ui.fragment;
import com.example.toutiao.ui.activity.SettingsActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.toutiao.R;
import com.example.toutiao.ui.activity.ArticleListActivity;
import com.example.toutiao.ui.activity.CartActivity;
import com.example.toutiao.ui.activity.OrderListActivity;
//我的模块
public class ProfileFragment extends Fragment {

    private String currentUsername;

    public static ProfileFragment newInstance(String username) {
        ProfileFragment fragment = new ProfileFragment();
        Bundle args = new Bundle();
        args.putString("username", username);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        if (getArguments() != null) {
            currentUsername = getArguments().getString("username");
        }

        ImageView imgAvatar = view.findViewById(R.id.imgAvatar);
        TextView tvUsername = view.findViewById(R.id.tvUsername);
        Button btnMyArticles = view.findViewById(R.id.btnMyArticles);
        Button btnMyOrders = view.findViewById(R.id.btnMyOrders);
        Button btnMyCart = view.findViewById(R.id.btnMyCart);
        Button btnSettings = view.findViewById(R.id.btnSettings);

        tvUsername.setText(currentUsername);
        // 设置头像（）
        imgAvatar.setImageResource(R.drawable.ic_user_avatar);

        // 我的文章
        btnMyArticles.setOnClickListener(v -> {
            // 获取当前用户的文章列表，并跳转到我的文章页面
            Intent intent = new Intent(getActivity(), ArticleListActivity.class);
            intent.putExtra("username", currentUsername); // 传递用户名，过滤用户文章
            startActivity(intent);
        });

        // 我的订单
        btnMyOrders.setOnClickListener(v -> {
            // 获取当前用户的订单列表，并跳转到我的订单页面
            Intent intent = new Intent(getActivity(), OrderListActivity.class);
            intent.putExtra("username", currentUsername); // 传递用户名，获取用户订单
            startActivity(intent);
        });

        // 我的购物车
        btnMyCart.setOnClickListener(v -> {
            // 跳转到购物车界面
            Intent intent = new Intent(getActivity(), CartActivity.class);
            startActivity(intent);
        });

        btnSettings.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), SettingsActivity.class);
            startActivity(intent);
        });

        return view;
    }
}
